<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-08
 * Time: 22:42
 */

namespace app\shell\controller;


use app\model\MoneyLog;
use app\tools\RedPacket;
use think\Db;

class RedPack {

    function __construct()
    {
        error_reporting(0);
    }

    function check(){

        $list = db('withdrawals_order')->where(['status'=> 10])->select();

        $red = new RedPacket();

        foreach ($list as $v){

            $result = $red->searchOrder([
               'mch_billno'=>  $v['order_no'],
            ]);

//            dump($result);

            if($result['status'] == 0) {

                db('withdrawals_order')->where(['id'=> $v['id']])->update(['status'=> 20, "wx_order_no"=> $result['detail_id']]);

            }elseif ($result['status'] == 1){


                Db::startTrans();

                try{
                    $total = $v['total_money']*$v['num'];
                    db('withdrawals_order')->where(['id'=> $v['id']])->update(['status'=> 30]);
                    db('customer')->where(['id'=> $v['customer_id']])->setInc("money", $total);

                    MoneyLog::addLog([
                        "child_id"=> 0,
                        "u_id"=> $v['customer_id'],
                        "type"=> 1,
                        "value"=> $total,
                        "reason_type"=> 4
                    ]);

                    Db::commit();

                }catch (\Exception $exception){
                    Db::rollback();
                    echo $exception->getMessage();
                }

            }

        }

    }

}